﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App.Contract
{
    public partial class frmInContract : Form
    {
        Entities db = new Entities();
        T_InnerCtr m_InnerCtr = null;

        private bool IsQuery = true;
        public int iID
        {
            set 
            { 
                var InnerCtr = db.T_InnerCtr.Where(a => a.iID == value);
                foreach (var innctr in InnerCtr)
                {
                    m_InnerCtr = innctr;
                    break;
                }
                if (m_InnerCtr == null) return;

                txtiCtrNo.Text = m_InnerCtr.iCtrNo;
                txtiSignat.Text = m_InnerCtr.iSignat;
                cbbfactory.Text = m_InnerCtr.factory;
                txtiDescript.Text = m_InnerCtr.iDescript;
                txtyName.Text = m_InnerCtr.yName;
                txtsGroup.Text = m_InnerCtr.sGroup;
                dtiCtrDate.ValueText = m_InnerCtr.iCtrDate.ToString();
                dtiConfirmDate.ValueText = m_InnerCtr.iConfirmDate.ToString();
                dtplanDate.ValueText = m_InnerCtr.planDate.ToString();
                dtFactOrderDate.ValueText = m_InnerCtr.factOrderDate.ToString();
                chkOrder.Checked = m_InnerCtr.bOrdered == "已下单" ? true : false;
                cbbXQC.Text = m_InnerCtr.xQcName;
                cbbGQC.Text = m_InnerCtr.gQcName;
                cbbKpFactory.Text = m_InnerCtr.kpFactory;

                //cbbdiliverPlace.Text =
                txtdiliverPlace.Text= m_InnerCtr.diliverPlace;
                //cbbdiliverMode.Text = 
                txtdiliverMode.Text=m_InnerCtr.diliverMode;
                //cbbdiliverCondition.Text = 
                txtdiliverCondition.Text=m_InnerCtr.diliverCondition;
                //cbbpayMode.Text = 
                txtpayMode.Text=m_InnerCtr.payMode;
                //cbbpack.Text = 
                txtpack.Text=m_InnerCtr.pack;
                //cbbchkAcp.Text = 
                txtchkAcp.Text=m_InnerCtr.chkAcp;
                //cbbOthers.Text = 
                txtOthers.Text=m_InnerCtr.others;
                //cbbMemo.Text = 
                txtMemo.Text=m_InnerCtr.Memo;
                cbbresolve.Text = m_InnerCtr.resolve;
            }
        }
        public frmInContract()
        {
            InitializeComponent();
            gvDataSource();
            this.panList.BringToFront();
        }
        //绑定PanList数据源
        private void gvDataSource()
        {
            var list = db.T_InnerCtr.ToList();
            bsList.DataSource = list;
            bn1.BindingSource = bsList;
        }
        //是否下单显示
        private void gv1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (gvList.Columns[e.ColumnIndex].Name=="colbOrdered" && e.Value!=null)
            {
                if (Convert.ToString(e.Value)=="已下单")
                {  
                    e.Value = true;
                }
            }
        }
        //概要、明细切换
        private void Button1_Click(object sender, EventArgs e)
        {
            iID = GetId();
            ShowPan(true);
           
            
        }
        //控制显示Pan
        private void ShowPan(Boolean b)
        {
            if (this.IsQuery)
            {
                this.panDetail.BringToFront();
                this.Button1.Text = "概要";
                this.IsQuery = false;
            }
            else
            {
                this.panList.BringToFront();
                this.Button1.Text = "明细";
                this.IsQuery = true;
            }
        }
  
        //获得选着行的内合同ID
        private int GetId()
        {
            if (gvList.RowCount > 0)
            {
                int index = gvList.CurrentRow.Index;
                return Convert.ToInt32(gvList.Rows[index].Cells["coliID"].Value.ToString());
            }
            else
            {
                return 0;
            }
        }
        //清空文本信息
        private void ClearTxt()
        {
            Helper.TxtClear(txtiCtrNo);
            Helper.TxtClear(txtiSignat);
            Helper.CBClear(cbbfactory);
            Helper.CBClear(txtiDescript);
            Helper.TxtClear(txtyName);
            Helper.TxtClear(txtsGroup);
           
            dtiCtrDate.ValueText = "";
            dtiConfirmDate.ValueText = "";
            dtplanDate.ValueText = "";
            dtFactOrderDate.ValueText = "";
            chkOrder.Checked = false;
            Helper.CBClear(cbbXQC);
            Helper.CBClear(cbbGQC);
            Helper.CBClear(cbbKpFactory);
            Helper.CBClear(cbbdiliverPlace);
            
            //txtdiliverPlace.Text=
            Helper.CBClear(cbbdiliverMode);
           
            //txtdiliverMode.Text=
            Helper.CBClear(cbbdiliverCondition);
           
            //txtdiliverCondition.Text=
            Helper.CBClear(cbbpayMode);
            
            //txtpayMode.Text=
            Helper.CBClear(cbbpack);
            
            //txtpack.Text=
            Helper.CBClear(cbbchkAcp);
            
            //txtchkAcp.Text=
            Helper.CBClear(cbbOthers);
            
            //txtOthers.Text=
            Helper.CBClear(cbbMemo);
            
            //txtMemo.Text=
            Helper.CBClear(cbbresolve);
            
        }
        //新增内合同
        private void AddButton_Click(object sender, EventArgs e)
        {
            m_InnerCtr = null;
            ShowPan(false);
            ClearTxt();
        }

        private void SavaButton_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("您确信保存订单信息吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                //if (Helper.TxtNotNull(txtiCtrNo) &&
                //    Helper.TxtNotNull(txtiSignat) &&
                //    string.IsNullOrEmpty(dtiCtrDate.ValueText) &&
                //    string.IsNullOrEmpty(dtiConfirmDate.ValueText) &&
                //    string.IsNullOrEmpty(dtFactOrderDate.ValueText)
                //    )
                //{


                    if (this.m_InnerCtr == null)
                    {

                        //保存新增信息
                        m_InnerCtr = new T_InnerCtr();
                        if (db.T_InnerCtr.Count() <= 0) m_InnerCtr.iID = 1;
                        else m_InnerCtr.iID = db.T_InnerCtr.OrderByDescending(a => a.iID).First().iID + 1;
                        m_InnerCtr.iCtrNo = txtiCtrNo.Text;
                        m_InnerCtr.iSignat = txtiSignat.Text;
                        m_InnerCtr.factory = cbbfactory.Text;
                        m_InnerCtr.iDescript = txtiDescript.Text;
                        m_InnerCtr.yName = txtyName.Text;
                        m_InnerCtr.sGroup = txtsGroup.Text;
                        if(!string.IsNullOrEmpty(dtiCtrDate.ValueText)) m_InnerCtr.iCtrDate = dtiCtrDate.Value;
                        if(!string.IsNullOrEmpty(dtiConfirmDate.ValueText)) m_InnerCtr.iConfirmDate = dtiConfirmDate.Value;
                        if(!string.IsNullOrEmpty(dtplanDate.ValueText)) m_InnerCtr.planDate = dtplanDate.Value;
                        if(!string.IsNullOrEmpty(dtFactOrderDate.ValueText)) m_InnerCtr.factOrderDate = dtFactOrderDate.Value;
                        m_InnerCtr.bOrdered = chkOrder.Checked == true ? "已下单" : "未下单";
                        m_InnerCtr.xQcName = cbbXQC.Text;
                        m_InnerCtr.gQcName = cbbGQC.Text;
                        m_InnerCtr.kpFactory = cbbKpFactory.Text;

                        m_InnerCtr.diliverPlace = cbbdiliverPlace.Text;
                        //txtdiliverPlace.Text=
                        m_InnerCtr.diliverMode = cbbdiliverMode.Text;
                        //txtdiliverMode.Text=
                        m_InnerCtr.diliverCondition = cbbdiliverCondition.Text;
                        //txtdiliverCondition.Text=
                        m_InnerCtr.payMode = cbbpayMode.Text;
                        //txtpayMode.Text=
                        m_InnerCtr.pack = cbbpack.Text;
                        //txtpack.Text=
                        m_InnerCtr.chkAcp = cbbchkAcp.Text;
                        //txtchkAcp.Text=
                        m_InnerCtr.others = cbbOthers.Text;
                        //txtOthers.Text=
                        m_InnerCtr.Memo = cbbMemo.Text;
                        //txtMemo.Text=
                        m_InnerCtr.resolve = cbbresolve.Text;

                        db.T_InnerCtr.Add(m_InnerCtr);
                        int i = db.SaveChanges();
                        if (i > 0)
                        {
                            Msg.ShowInfo(string.Format("已保存{0}条订单信息。", i));
                            gvDataSource();
                            this.panList.BringToFront();
                            this.Button1.Text = "明细";
                            this.IsQuery = true;
                        }

                    }
                    else
                    {
                        //保存修改信息
                        m_InnerCtr.iCtrNo = txtiCtrNo.Text;
                        m_InnerCtr.iSignat = txtiSignat.Text;
                        m_InnerCtr.factory = cbbfactory.Text;
                        m_InnerCtr.iDescript = txtiDescript.Text;
                        m_InnerCtr.yName = txtyName.Text;
                        m_InnerCtr.sGroup = txtsGroup.Text;
                        if (dtiCtrDate.Value != null) m_InnerCtr.iCtrDate = dtiCtrDate.Value;
                        if (dtiConfirmDate.Value != null) m_InnerCtr.iConfirmDate = dtiConfirmDate.Value;
                        if (dtplanDate.Value != null) m_InnerCtr.planDate = dtplanDate.Value;
                        if (dtFactOrderDate.Value != null) m_InnerCtr.factOrderDate = dtFactOrderDate.Value;
                        m_InnerCtr.bOrdered = chkOrder.Checked == true ? "已下单" : "未下单";
                        m_InnerCtr.xQcName = cbbXQC.Text;
                        m_InnerCtr.gQcName = cbbGQC.Text;
                        m_InnerCtr.kpFactory = cbbKpFactory.Text;

                        m_InnerCtr.diliverPlace = cbbdiliverPlace.Text;
                        //txtdiliverPlace.Text=
                        m_InnerCtr.diliverMode = cbbdiliverMode.Text;
                        //txtdiliverMode.Text=
                        m_InnerCtr.diliverCondition = cbbdiliverCondition.Text;
                        //txtdiliverCondition.Text=
                        m_InnerCtr.payMode = cbbpayMode.Text;
                        //txtpayMode.Text=
                        m_InnerCtr.pack = cbbpack.Text;
                        //txtpack.Text=
                        m_InnerCtr.chkAcp = cbbchkAcp.Text;
                        //txtchkAcp.Text=
                        m_InnerCtr.others = cbbOthers.Text;
                        //txtOthers.Text=
                        m_InnerCtr.Memo = cbbMemo.Text;
                        //txtMemo.Text=
                        m_InnerCtr.resolve = cbbresolve.Text;
                        int i = db.SaveChanges();
                        if (i > 0)
                        {
                            Msg.ShowInfo(string.Format("已保存{0}条订单信息。", i));
                            gvDataSource();
                            this.panList.BringToFront();
                            this.Button1.Text = "明细";
                            this.IsQuery = true;
                        }
                    }
                //}
                //else
                //{
                //    Msg.ShowError("请填写必填项！");
                //}
            }
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("您确信删除信息吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                if (GetId()!=0)
                {
                    int id = GetId();
                    var ICtr = db.T_InnerCtr.FirstOrDefault(a => a.iID == id);
                    if (ICtr != null)
                    {
                       db.T_InnerCtr.Remove(ICtr);
                       var i = db.SaveChanges();
                       if (i>0)
                       {
                             Msg.ShowInfo(string.Format("已删除{0}条订单信息。", i));
                             gvDataSource();
                       }
                    }
                }
                else
                {
                    Msg.ShowError("请选择一条记录！");
                    return;
                }
            }
        }

        private void EditButton_Click(object sender, EventArgs e)
        {
            iID = GetId();
            ShowPan(false);
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("尚未保存，你确定要取消吗？")==System.Windows.Forms.DialogResult.Yes)
            {
                ShowPan(true);
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            var list = db.T_InnerCtr.Where(a => a.iCtrNo == txtCtrNo.Text);
            bsList.DataSource = list.ToList();
        }

        private void GetBaseInfo()
        {
            Service.Sfactory(cbbfactory);
            //Service.XQC(cbbXQC); 
            //Service.GQC(cbbGQC);
            Service.kpFactory(cbbKpFactory);
            Service.DiliverPlace(cbbdiliverPlace);
            Service.DiliverMode(cbbdiliverMode);
            Service.DiliverCondition(cbbdiliverCondition);
            Service.PayMode(cbbpayMode);
            Service.Pack(cbbpack);
            Service.ChkAcp(cbbchkAcp);
            Service.Others(cbbOthers);
            Service.Memo(cbbMemo);
            Service.Resolve(cbbresolve);
        }

        private void frmInContract_Load(object sender, EventArgs e)
        {
            GetBaseInfo();
        }

        private void cbbdiliverPlace_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (sender == cbbdiliverPlace)
            {
                if (cbbdiliverPlace.SelectedIndex == -1) return;
                txtdiliverPlace.Text = cbbdiliverPlace.SelectedValue.ToString();
            }
            else if (sender == cbbdiliverMode)
            {
                if (cbbdiliverMode.SelectedIndex == -1) return;
                txtdiliverMode.Text = cbbdiliverMode.SelectedValue.ToString();
            }
            else if (sender == cbbdiliverCondition)
            {
                if (cbbdiliverCondition.SelectedIndex == -1) return;
                txtdiliverCondition.Text = cbbdiliverCondition.SelectedValue.ToString();
            }
            else if (sender == cbbpayMode)
            {
                if (cbbpayMode.SelectedIndex == -1) return;
                txtpayMode.Text = cbbpayMode.SelectedValue.ToString();
            }
            else if (sender == cbbpack)
            {
                if (cbbpack.SelectedIndex == -1) return;
                txtpack.Text = cbbpack.SelectedValue.ToString();
            }
            else if (sender == cbbchkAcp)
            {
                if (cbbchkAcp.SelectedIndex == -1) return;
                txtchkAcp.Text = cbbchkAcp.SelectedValue.ToString();
            }
            else if (sender == cbbOthers)
            {
                if (cbbOthers.SelectedIndex == -1) return;
                txtOthers.Text = cbbOthers.SelectedValue.ToString();
            }
            else if (sender == cbbMemo)
            {
                if (cbbMemo.SelectedIndex == -1) return;
                txtMemo.Text = cbbMemo.SelectedValue.ToString();
            }
        }
    }
}
